﻿using Mes.原料管理.Models;
using Mes.采购管理.model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.AxHost;

namespace Mes.原料管理
{
    public partial class EditForm : Form
    {
        private string _materialId;

        public EditForm(string materialId)
        {
            InitializeComponent();
            _materialId = materialId;
            LoadMaterialData();
        }
        private async void LoadMaterialData()
        {
            try
            {
                using (HttpClient client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Authorization =
                       new AuthenticationHeaderValue("Bearer", login.CurrentToken);
                    client.BaseAddress = new Uri("http://10.223.8.19:8045/");
                    HttpResponseMessage response = await client.GetAsync($"api/Materials/GetMaterialValue?Id={_materialId}");

                    if (response.IsSuccessStatusCode)
                    {
                        string json = await response.Content.ReadAsStringAsync();
                        if (string.IsNullOrEmpty(json))
                        {
                            MessageBox.Show("API 返回的数据为空！");
                            return;
                        }
                        var apiResponse = JsonConvert.DeserializeObject<MaterialsApiResponsess>(json);
                        // 检查 material 是否为
                        if (apiResponse?.code != 200 || apiResponse.data == null|| apiResponse.data.Count == 0)
                        {
                            MessageBox.Show("API 返回的数据无效！");
                            return;
                        }
                        var material = apiResponse.data[0];
                        textupdateCode.Text = material.materialNumber.ToString();
                        textupdateName.Text = material.materialName.ToString();
                        comupdateMaterialclassification.Text = material.materialclassification.ToString();
                        textupdateSize.Text = material.size.ToString();
                        textupdateSpecification.Text = material.specification.ToString();
                        comupdateAddrNo.Text = material.addrNo.ToString();
                        textupdateArea.Text = material.area.ToString();
                        textupdateKucunNum.Text=material.kucunNum.ToString();
                        comupdateMaterialType.Text = material.materialType.ToString();
                        comupdateState.Text = material.state.ToString();
                        textupdateRemark.Text = material.remark.ToString();
                        textupdateMaterialNum.Text = material.materialNum.ToString();   
                        textupdateInventorycap.Text=material.inventorycap.ToString();
                        textupdateInventoryfloor.Text=material.inventoryfloor.ToString();
                        textupdateMaterialLocation.Text=material.materialclassification.ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"加载数据失败：{ex.Message}");
            }
        }

        private async void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                var material = new
                {
                    Id = _materialId,
                    MaterialNumber= textupdateCode.Text,
                    MaterialName = textupdateName.Text,
                    Materialclassification = comupdateMaterialclassification.Text,
                    Size= textupdateSize.Text,
                    Specification= textupdateSpecification.Text,
                    AddrNo= comupdateAddrNo.Text,
                    Area= textupdateArea.Text,
                    KucunNum= textupdateKucunNum.Text,
                    MaterialType= comupdateMaterialType.Text,
                    State= comupdateState.Text,
                    Remark= textupdateRemark.Text,
                    MaterialNum= textupdateMaterialNum.Text,
                    Inventorycap= textupdateInventorycap.Text,
                    Inventoryfloor= textupdateInventoryfloor.Text,
                    MaterialLocation= textupdateMaterialLocation.Text,
                };

                string json = JsonConvert.SerializeObject(material);
                var content = new StringContent(json, Encoding.UTF8, "application/json");

                using (HttpClient client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Authorization =
                       new AuthenticationHeaderValue("Bearer", login.CurrentToken);
                    client.BaseAddress = new Uri("http://10.223.8.19:8045/");
                    HttpResponseMessage response = await client.PostAsync(
                        "api/Materials/UpdateMaterial",
                        content);

                    if (response.IsSuccessStatusCode)
                    {
                        MessageBox.Show("修改成功！");
                        this.DialogResult = DialogResult.OK; // 通知主窗体刷新数据
                        this.Close();
                    }
                    else
                    {
                        string error = await response.Content.ReadAsStringAsync();
                        MessageBox.Show($"修改失败：{response.StatusCode}\n{error}");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"保存失败：{ex.Message}");
            }
        }

   
    }
    
}
